//
//  ResourcesViewController+Adapter.m
//  CareGiver
//
//  Created by Guillermo Moral on 8/23/14.
//  Copyright (c) 2014 Humana. All rights reserved.
//

#import "ResourcesViewController+Adapter.h"



#define DUMMY YES;

@implementation ResourcesViewController (Adapter)

-(NSArray*)buildSearchAddressDictionary {
    
    NSMutableArray *mArray = [[NSMutableArray alloc]initWithCapacity:1];
    NSMutableDictionary *mDictionary = [[NSMutableDictionary alloc]initWithCapacity:7];
    
    // TODO parsear todo
    //[mDictionary setObject:self.searchAddress.keyword forKey:@"address1"];
    
    [mArray addObject:mDictionary];
    return mArray;
}

-(void)loadResourcesData {
    
    if(!self.resourcesData) {
        self.resourcesData = [NSMutableArray new];
    } else {
        [self.resourcesData removeAllObjects];
    }
  
    RestServiceClient *client = [[RestServiceClient alloc]initWithDelegate:self serviceNamespace:@"DummyService" serviceName:@"GetResources"];
        
    [client.params setObject:@"pepe" forKey:@"keyword"];
    self.smServiceCallInProgress = YES;
    [client executeRequest];
}

-(void)loadMoreResourcesData {
    if(!self.smServiceCallInProgress && ([self.resourcesData count] < self.totalNumberOfResources)) {
        self.lastMessageRow+=7;
        
        RestServiceClient *client = [[RestServiceClient alloc]initWithDelegate:self serviceNamespace:@"DummyService" serviceName:@"GetResources"];
        self.smServiceCallInProgress = YES;
        [client executeRequest];
    }
}

# pragma  RestService

-(void)serviceRequestReceiveData:(id)data {
    //[self loadResourcesFromDictionary:[data valueForKey:@"data"]];
    self.totalNumberOfResources = 100;
    for(ResourceModel *rm in data) {
        [self.resourcesData addObject:rm];
    }
    
    self.smServiceCallInProgress = NO;
    [self.tbl_resourceList reloadData];
}

-(void)serviceRequestFailedWithError:(NSError*)error {
    NSLog(@"Error!");
}

-(void)loadResourcesFromDictionary:(NSDictionary*)dictionary {
    NSDictionary *resourceList = [dictionary valueForKey:@"resource"];
    
    if(resourceList && ![resourceList isEqual:[NSNull null]]) {
        self.hasData = YES;
        self.message.hidden = NO;
        self.totalNumberOfResources = 54;
        
        for(NSArray *resources in [dictionary valueForKey:@"resources"]){
            ResourceModel *resourceModel = [[ResourceModel alloc]initKey:@"" organization:@"" address1:@"" address2:@""latitude:@"" longitude:@""];
            [self.resourcesData addObject:resourceModel];
        }
                                       
    } else {
        self.hasData = NO;
        self.message.hidden = NO;
    }
    
    self.smServiceCallInProgress = NO;
    [self.tbl_resourceList reloadData];
}

@end
